﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SWEN3Application.Classes;
using System.Text.RegularExpressions;

namespace SWEN3Application
{
    public partial class CheckIn : MasterForm
    {
        private Rooms r;
        private BookingList b;
        public CheckIn()
        {
            InitializeComponent();
        }

        private void AdultsNumeric_ValueChanged(object sender, EventArgs e)
        {
            if (AdultsNumeric.Value == 0)
            {
                ChildsNumeric.Enabled = false;
                ChildsNumeric.Value = 0;
            }
            else
            {
                ChildsNumeric.Enabled = true;
            }
        }

        private void Book_Click(object sender, EventArgs e)
        {
            try
            {
                if (CustomValidate())
                {
                    BookingList b = new BookingList();
                    b.LastName = LastName.Text;
                    b.FirstName = FirstName.Text;
                    b.AdultsNo = (int)AdultsNumeric.Value;
                    b.ChildsNo = (int)ChildsNumeric.Value;
                    b.PhoneNo = PhoneNo.Text;
                    b.EmailAddress = EmailAddress.Text;
                    b.Address = HomeAddress.Text;
                    b.Remarks = Remarks.Text;
                    b.Country = Country.Text;
                    Rooms r = this.r;
                    b.BookingNo = BookingDBManager.CheckIn(b);
                    if (b.BookingNo > 0)
                    {
                        r.BookingNo = b.BookingNo;
                        r.RoomStatus = "Occupied";
                        r.CheckIn = System.DateTime.Now;
                        r.CheckOut = DateTime.MinValue;
                        r.StaffId = 0;

                        housekeepingOscar h = HousekeepingDBManager.GetRoomByRoomNo(r.RoomNo);
                        h.roomStatus = r.RoomStatus;
                        h.roomType = r.RoomType;
                        HousekeepingDBManager.UpdateHousekeeping(h);

                        if (RoomDBManager.updateRooms(r))
                        {
                            DetailsForm details = new DetailsForm(b, r, null);
                            details.Text = "Check In Details";
                            this.b = b;
                            details.ShowDialog();
                            Book.Enabled = false;
                            RePrint.Visible = true;
                        }
                        else
                        {
                            MessageBox.Show("Room Update Fail!");
                            BookingDBManager.DeleteBookingByBookingNo(r.BookingNo);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Create Booking Fail!");
                    }
                }
            }
            //This is to catch all types of exception that is not handled by the if else. 
            //Also to ensure the application is still running.
            catch (Exception ex)
            {
                MessageBox.Show("A serious error has occured during checkin process.");
            }
        }

        private Boolean CustomValidate()
        {
            Boolean IsValid = true;
            errorProvider.Clear();
            foreach (Control ctrl in CustomerInformationBox.Controls)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null)
                {
                    if (String.IsNullOrEmpty(tb.Text))
                    {
                        if (tb.Name != "Remarks")
                        {
                            IsValid = false;
                            errorProvider.SetError(tb, "The field is empty!");
                        }
                    }
                    else if (tb.Name == "EmailAddress")
                    {

                        if (!Regex.IsMatch(tb.Text, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
                        {
                            IsValid = false;
                            errorProvider.SetError(tb, "Invalid email address format!");
                        }
                    }
                }

            }
            if (r == null)
            {
                IsValid = false;
                RoomInfo rm = new RoomInfo(setRm, "Vacant");
                rm.Owner = this;
                rm.ShowDialog();
            }
            return IsValid;
        }

        private void Check_Click(object sender, EventArgs e)
        {
            RoomInfo room = new RoomInfo(setRm, "Vacant");
            room.Owner = this;
            room.ShowDialog();
        }

        private void setRm(Rooms r)
        {
            if (r == null)
            {
                RoomNo.Text = "";
            }
            else
            {
                RoomNo.Text = r.RoomNo.ToString();
            }
            this.r = r;
        }

        private void Reset_Click(object sender, EventArgs e)
        {
            Book.Enabled = true;
            RePrint.Visible = false;
            this.b = null;
            foreach (Control ctrl in CustomerInformationBox.Controls)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null) tb.Text = "";
            }
            RoomNo.Text = "";
            r = null;
            errorProvider.Clear();
        }

        private void RePrint_Click(object sender, EventArgs e)
        {
            DetailsForm details = new DetailsForm(b, r, null);
            details.Text = "Check In Details";
            details.ShowDialog();
            Book.Enabled = false;
        }
    }
}

